package com.baosight.xcyber.tm.re.domain;

import com.alibaba.fastjson.JSONObject;
import com.zeroc.IceGrid.AdapterDescriptor;
import com.zeroc.IceGrid.ObjectDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class UIAdapter extends UITreeNode{
    public static Logger logger= LoggerFactory.getLogger(UIAdapter.class);
    AdapterDescriptor adapterDescriptor=null;
    public UIAdapter(AdapterDescriptor adapterDescriptor){
        this.type="ADAPTER";
        this.adapterDescriptor=adapterDescriptor;
        this.name=adapterDescriptor.name;
        param= (JSONObject) JSONObject.toJSON(adapterDescriptor);
        List<JSONObject> collectObject = objectsToJSONList(adapterDescriptor.objects);
        param.put("objects",collectObject);
        List<JSONObject> collectAllocatables = objectsToJSONList(adapterDescriptor.allocatables);
        param.put("allocatables",collectAllocatables);
    }


    public static List<JSONObject> objectsToJSONList(List<ObjectDescriptor> objectDescriptors){
        return objectDescriptors.stream().map(item -> new JSONObject() {{
            put("identity", item.id.category + "/" + item.id.name);
            put("type", item.type);
        }}).collect(Collectors.toList());
    }
}
